﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using UI.Comm.Controls;
using PersonalHealth.Data.IService;
using PersonalHealth.Data.Service;
using HealthManagement.Common;

namespace HealthManagement.Views
{
    /// <summary>
    /// ChangePassword.xaml 的交互逻辑
    /// </summary>
    public partial class ChangePassword : PMWindow
    {
        public ChangePassword()
        {
            InitializeComponent();
        }

        private void Save_Click(object sender, RoutedEventArgs e)
        {
            string origin = tbOrigin.Password;
            string newPwd = tbNew.Password;
            string confirmPwd = tbConfirm.Password;

            if (newPwd != confirmPwd)
            {
                MessageBox.Show("输入的密码不一致，请重新输入");
                tbNew.Clear();
                tbConfirm.Clear();
                tbOrigin.Clear();
                return;
            }

            if( Convert.ToBase64String(Encoding.Default.GetBytes(origin)) != AppData.User.Password)
            {
                MessageBox.Show("原始密码错误，请重新输入");
                tbNew.Clear();
                tbConfirm.Clear();
                tbOrigin.Clear();
                return;
            }
            IUserService ser = new UserService();
            var user = AppData.User;
            user.Password = newPwd;
            ser.Update(user);
            AppData.User = user;

            this.Close();
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
